package util.android.form.respuesta;

import util.android.cuestionario.fragment.PageView;
import util.android.form.ListViewFormAdapter;
import util.android.window.AceptarCancelarDialog;
import util.android.window.OnAceptarCancelarListener;
import util.form.CuestionarioContext;
import util.form.respuesta.Input;
import util.form.respuesta.RegistroImpl;
import util.form.respuesta.listener.OnGuardarRegistroListener;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.androidutil.R;

public class ItemListView {

	private final ListViewInput mListView;
	private final String mKey;
	private final String mEtiqueta;
	private final TableRow tr;
	//private final CheckBoxInput cbInput;
	private final CheckBox cb;
	private final Button btn;
	private final TextView tvEtiqueta;
	private boolean relevancia = true;
	
	public ItemListView(ListViewInput listView, String key, String etiqueta) {
		this.mListView = listView;
		this.mKey = key;
		this.mEtiqueta = etiqueta;
		tr =  (TableRow) View.inflate(listView.getView().getContext(), R.layout.view_list_form, null);
		cb = (CheckBox) tr.findViewWithTag("check");
		//cb.setTag(key);
		//cbInput = new CheckBoxInput(cb);
		
		btn = (Button) tr.findViewWithTag("boton");
		tvEtiqueta = (TextView) tr.findViewWithTag("texto");
				
		//cb.setText(key);
		tvEtiqueta.setText(key+" "+etiqueta);
		
		btn.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {	
				try {
					mListView.apuntarItem(mKey);
					mostrarDialog();
				} catch (Exception e) {
					mListView.getCuestionarioContext().manejarExcepcion(e);
				}
							
			}
		});
		
		
		
	}

	public TableRow getTr() {
		return tr;
	}

	public void setRelevancia(boolean relevancia) {
		if(relevancia) {
			btn.setVisibility(View.VISIBLE);
		}
		else {
			btn.setVisibility(View.INVISIBLE);
		}
		this.relevancia = relevancia;
	}

	public String getKey() {
		return mKey;
	}
	
	public Button getBtn() {
		return btn;
	}
	

	public RegistroImpl getRegistroSinRelevancia() {
		PageView pv = crearPageView().pv;
		for(Input in: pv.getInputsTotal()) {
			in.setRelevancia(false);
		}		
		return pv.getRegistro();
	};
	
	public PageViewDialog crearPageView() {
		ListViewFormAdapter formAdapter = mListView.getAdapter();
		AceptarCancelarDialog d = new AceptarCancelarDialog(mListView.getView().getContext(),null);
		d.setContentView(formAdapter.getContentView());		
		CuestionarioContext cuestionarioContext = mListView.getCuestionarioContext();
		final PageView pv = new PageView(cuestionarioContext,1,d.getLayout(),null); 
		try {				
			formAdapter.onCreateDialog(mKey,pv);
		} catch (Exception e) {
			cuestionarioContext.manejarExcepcion(e);
		}
		
		PageViewDialog pvd = new PageViewDialog();
		pvd.pv = pv;
		pvd.d = d;
		return pvd;
	}

	public PageView mostrarDialog() {
		final ListViewFormAdapter formAdapter = mListView.getAdapter();		
				
		final PageViewDialog pvd = crearPageView();
		AceptarCancelarDialog d = pvd.d;
		final PageView pv = pvd.pv;
		
		d.setTitle(formAdapter.getTitulo(mKey,mEtiqueta));
		
		//http://stackoverflow.com/questions/18008821/cannot-change-the-android-dialog-size
		d.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
				
		pv.setOnGuardarListener(new OnGuardarRegistroListener() {
			@Override
			public void onGuardar(RegistroImpl reg) throws Exception {
				formAdapter.onGuardar(mKey, reg);				
				mListView.abrirProximoItemRelevanteIncompleto();				
			}
		});
		
		d.setOnAceptarCancelarListener(new OnAceptarCancelarListener() {
			
			@Override
			public void onCerrar() throws Exception {
				
			}
			
			@Override
			public void onCancelar() throws Exception {
				formAdapter.onCancelar(mKey);
			}
			
			@Override
			public boolean onAceptar() throws Exception {				
				pv.guardar();				
				cb.setChecked(true);//NOTA: despues de guardar se debe hacer check. No antes
				return true;
			}
		});
		
		EditTextInput in = mListView.getEditTextInput(mKey);
		if(in!=null) {
			in.setRelevancia(true);
			if(!in.getValor().isEmpty())
				d.show();
		}
		else {
			d.show();
		}
		
		return pv;
	}

	public boolean isRelevante() {
		return relevancia;
	}

////////////////////////////////////////////	
	private class PageViewDialog {
		public PageView pv;
		public AceptarCancelarDialog d;		
	}

	public TextView getTvEtiqueta() {
		return tvEtiqueta;
	}

	public CheckBox getCb() {
		return cb;
	}

	
}
